package com.zttek.photosShareProject.modules.web.share;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.base.MyBaseController;
import com.zttek.photosShareProject.common.json.CommentVO;
import com.zttek.photosShareProject.modules.model.Comment;
import com.zttek.photosShareProject.modules.service.share.IAbuseService;
import com.zttek.photosShareProject.modules.service.share.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Y
 * Date: 2017-08-30
 * Time: 9:54
 */
@RequestMapping("/comment")
@Controller
public class CommentController extends MyBaseController {

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IAbuseService abuseService;

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public Object getList(@RequestBody CommentVO.GetListForm form) throws Exception {
        Page<Comment> page = new Page<>(form.getPageNum(), form.getPageSize(), form.getOrderByField());
        page.setAsc(form.isAscend());
        commentService.getShareComment(page, form.getShareId());
        List<Comment> comments = page.getRecords();
        List<CommentVO> list = commentService.comment2VO(comments, form.getUserId(), false);
        return renderSuccess(list);
    }

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    @ResponseBody
    public Object newComment(@RequestBody CommentVO.createForm form) throws Exception{
        Comment comment = commentService.newReply(form.getContent(), form.getAtIds(), form.getShareId(),
                form.getUserId(), form.getTargetId());
        CommentVO commentVO = commentService.comment2VO(comment, form.getUserId(), false);
        return renderSuccess(commentVO);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam("commentId") Long commentId) throws Exception {
        commentService.deleteComment(commentId);
        return renderSuccess();
    }

    @RequestMapping(value = "/praise", method = RequestMethod.POST)
    @ResponseBody
    public Object praise(@RequestBody CommentVO.RelevanceForm form) throws Exception {
        commentService.praise(form.getCommentId(), form.getUserId(), form.isDirection());
        return renderSuccess();
    }

    @RequestMapping(value = "/abuse", method = RequestMethod.POST)
    @ResponseBody
    public Object abuse(@RequestParam("commentId") Long commentId,
                        @RequestParam("userId") Long userId,
                        @RequestParam("reason") String reason) throws Exception {
        abuseService.abuseComment(commentId, userId, reason);
        return renderSuccess();
    }
}
